SportsClient copy 7.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. "use client";
  2. import { FC, useEffect, useRef, useState } from "react";
  3. import { useRouter } from "@/i18n/routing"; // Assuming router logic
  4. import { useTranslations } from "next-intl"; // Assuming translations
  5. interface Props {
  6. brand_id: string;
  7. token: string;
  8. }
  9. const SportsClient: FC<Props> = (props) => {
  10. const { brand_id, token } = props;
  11. const [currentToken, setCurrentToken] = useState(token);
  12. const btRef = useRef(null);
  13. const router = useRouter();
  14. const isFreshRef = useRef(false);
  15. // Effect to dynamically load the script on client mount
  16. useEffect(() => {
  17. if (typeof window !== "undefined") { // Make sure it's only executed on the client
  18. const script = document.createElement("script");
  19. script.src = "https://ui.invisiblesport.com/bt-renderer.min.js";
  20. script.async = true;
  21. script.onload = () => {
  22. console.log("onLoad=====> Script Loaded and Initialized");
  23. onLoad();
  24. };
  25. document.body.appendChild(script);
  26. // Cleanup when the component is unmounted
  27. return () => {
  28. console.log("onLoad=====> Clean up");
  29. if (btRef.current) {
  30. // @ts-ignore
  31. btRef.current?.kill();
  32. btRef.current = null;
  33. }
  34. document.body.removeChild(script); // Remove script when component unmounts
  35. };
  36. }
  37. }, [currentToken]); // Trigger the effect when `currentToken` changes
  38. // Update token and re-initialize if necessary
  39. const updateToken = (newToken: string) => {
  40. if (currentToken !== newToken) {
  41. setCurrentToken(newToken);
  42. // @ts-ignore
  43. if (window.BTRenderer) {
  44. onLoad();
  45. }
  46. }
  47. };
  48. // Handle initialization when script is loaded
  49. const onLoad = () => {
  50. // @ts-ignore
  51. const bt = new BTRenderer();
  52. btRef.current = bt;
  53. console.log("onLoad=====> Initialized with token:", token);
  54. bt.initialize({
  55. brand_id: brand_id,
  56. token: currentToken,
  57. onTokenExpired: () => console.log("Token expired, handle refresh"),
  58. onSessionRefresh: () => {
  59. console.log("onSessionRefresh triggered");
  60. if (btRef.current) {
  61. // @ts-ignore
  62. btRef.current?.kill();
  63. }
  64. isFreshRef.current = true;
  65. router.refresh();
  66. },
  67. themeName: "default",
  68. lang: "pt-br",
  69. target: document.getElementById("betby"),
  70. betSlipOffsetBottom: 80,
  71. betSlipZIndex: 1000,
  72. stickyTop: 0,
  73. betSlipOffsetTop: 50,
  74. onRecharge: function () {
  75. router.push("/deposit");
  76. },
  77. });
  78. };
  79. return <div id="betby" className="h-[100%]"></div>;
  80. };
  81. export default SportsClient;